package com.platform.common.entity;

/**
 * <p>
 *
 * </p>
 *
 * @author： yuanlai
 * @since 2021-06-22
 */

import com.platform.common.enums.ResponseCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Objects;

/**
 * 定义通用返回响应：统一的返回数据
 *  类名，也可以命名为ServerResponseEntity。
 *
 * @author: yuanlai
 * @date 2020/7/3
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommonResponse<T> implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(CommonResponse.class);
    @ApiModelProperty("状态码,000000为成功")
    private String code;
    @ApiModelProperty("信息")
    private String msg;
    @ApiModelProperty("数据")
    private T data;

    public CommonResponse(ResponseCodeEnum responseCodeEnum){
        this.code = responseCodeEnum.getCode();
        this.msg = responseCodeEnum.getMsg();
    }
    public CommonResponse(ResponseCodeEnum responseCodeEnum, T data){
        this.code = responseCodeEnum.getCode();
        this.msg = responseCodeEnum.getMsg();
        this.data = data;
    }
    public CommonResponse(String code, String message){
        this.code = code;
        this.msg = message;
    }
    public CommonResponse(String code, T data, String message){
        this.code = code;
        this.data = data;
        this.msg = message;
    }
    public static <T> CommonResponse create(ResponseCodeEnum responseCodeEnum){
        return new CommonResponse(responseCodeEnum);
    }
    public static <T> CommonResponse create(ResponseCodeEnum responseCodeEnum, T data){
        return new CommonResponse(responseCodeEnum, data);
    }
    public static <T> CommonResponse error(String message){
        return new CommonResponse(ResponseCodeEnum.ERROR.getCode(), message);
    }


    public static <T> CommonResponse<T> success(T data) {
        return CommonResponse.<T>builder()
                .code(ResponseCodeEnum.OK.value())
                .data(data).build();
    }

    public static <T> CommonResponse<T> success() {
        return CommonResponse.<T>builder()
                .code(ResponseCodeEnum.OK.value())
                .msg(ResponseCodeEnum.OK.getMsg()).build();
    }

    /**
     * 前端显示失败消息
     *
     * @param msg 失败消息
     * @return
     */
    public static <T> CommonResponse<T> showFailMsg(String msg) {
        log.error(msg);
        return CommonResponse.<T>builder()
                .code(ResponseCodeEnum.SHOW_FAIL.value())
                .msg(msg).build();
    }

    public static <T> CommonResponse<T> fail(ResponseCodeEnum responseCodeEnum) {
        log.error(responseCodeEnum.toString());
        return CommonResponse.<T>builder()
                .code(responseCodeEnum.value())
                .msg(responseCodeEnum.getMsg()).build();
    }

    public static <T> CommonResponse<T> fail(ResponseCodeEnum responseCodeEnum, T data) {
        log.error(responseCodeEnum.toString());
        return CommonResponse.<T>builder()
                .code(responseCodeEnum.value())
                .msg(responseCodeEnum.getMsg())
                .data(data).build();
    }

    public static <T> CommonResponse<T> transform(CommonResponse<?> oldCommonResponse) {
        CommonResponse<T> commonResponse = CommonResponse.<T>builder()
                .code(oldCommonResponse.getCode())
                .msg(oldCommonResponse.getMsg()).build();
        log.error(commonResponse.toString());
        return commonResponse;
    }

    public boolean isSuccess() {
        return Objects.equals(ResponseCodeEnum.OK.value(), this.code);
    }

    @Override
    public String toString() {
        return "ServerResponseEntity{" + "code=" + code + ", msg='" + msg + "'" + ", data=" + data + '}';
    }
}
